package com.jiyun.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.dto.EmpDto;
import com.jiyun.entity.Dept;
import com.jiyun.entity.Emp;
import com.jiyun.mapper.DeptMapper;
import com.jiyun.mapper.EmpMapper;
import com.jiyun.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-27
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {

    @Autowired
    EmpMapper empMapper;

    @Autowired
    DeptMapper deptMapper;

    @Override
    public Page<EmpDto> findPage(Integer page, Integer pageSize, EmpDto empDto) {

        Page<EmpDto> empDtoPage = new Page<>(page, pageSize);

        empMapper.findPage(empDtoPage,empDto);

        //查询部门名称的
        for (EmpDto record : empDtoPage.getRecords()) {

            String[] dids = record.getDeptIds().split(",");

            String name = "";

            for (String did : dids) {
                Dept dept = deptMapper.selectById(did);
                name += dept.getName()+"-";
            }
            name = name.substring(0,name.length()-1);
            record.setDeptNames(name);
        }

        return empDtoPage;
    }

    @Override
    public String findMaxIdData() {
        return empMapper.findMaxIdData();
    }
}
